package com.xcx.demo.controller;

import com.xcx.demo.entity.Product;
import com.xcx.demo.result.Result;
import com.xcx.demo.result.ResultFactory;
import com.xcx.demo.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ProductController {

    @Autowired
    ProductService productService;

    @CrossOrigin
    @PostMapping("/products/{product_id}")
    public Result getProduct(@PathVariable Integer product_id){//根据商品id查询商品并返回
        if (productService.isExist(product_id))
            {return ResultFactory.buildSuccessResult(productService.findById(product_id));}
        else
        {return  ResultFactory.buildFailResult("商品不存在");}
    }

    @CrossOrigin
    @GetMapping("/products/search")
    public Result searchResult(@RequestParam("keywords") String keywords){
        if ("".equals(keywords)){
            return ResultFactory.buildSuccessResult(productService.list());
        }else {
            return ResultFactory.buildSuccessResult(productService.search(keywords));
        }
    }
}
